#!/bin/bash
#
# uninstall-sh
#
# note: this file is not named uninstall.sh to avoid unwanted behavior
# due to implicit rules with "make"
#
# uninstaller for joy and related files

echo
echo JOY UNINSTALL-SH
echo --------------------
echo

delete_file () {
    if [ -f $1 ]; then
        rm $1
        retval=$?
        if [ $retval != "0" ]; then
            echo "error: could not delete file $1"
        else
            echo "deleted file $1"
        fi
    fi
}

delete_directory () {
    if [ -d $1 ]; then
        rm -rf $1
        retval=$?
        if [ $retval != "0" ]; then
            echo "error: could not delete directory $1"
        else
            echo "deleted directory $1"
        fi
    fi
}

sysname=`uname -s`

if [ "$sysname" == "Darwin" ]; then
    ##
    # Darwin operating system detected
    ##

    pkgutil --pkg-info com.cisco.joy 2>/dev/null
    if [ $? == 0 -o -f /usr/local/bin/uninstall-joy-pkg ]; then
        echo
        echo "You appear to have installed the Joy binary package."
        echo "Please run the /usr/local/bin/uninstall-joy-pkg script."
        echo
        exit 1
    fi
    echo "System $sysname (Mac OS X) uninstalling ..."

    # Stop the daemon
    launchctl unload /Library/LaunchDaemons/joy.plist
    launchctl unload /Library/LaunchAgents/com.cisco.joy.plist

    # Delete the application files
    delete_file /usr/local/bin/joy

    delete_file /usr/local/lib/libjoy.a
    delete_file /usr/local/include/joy/joy_api.h

    delete_file /usr/local/bin/sleuth

    delete_directory /usr/local/include/joy

    delete_directory /usr/local/etc/joy

    delete_file /Library/LaunchDaemons/joy.plist
    delete_file /Library/LaunchAgents/com.cisco.joy.plist

    delete_file /usr/local/share/man/man1/joy.1

    # Uninstall the sleuth python module
    pip uninstall -y sleuth
    echo "... done"

    echo
elif [ "$sysname" == "Linux" ]; then
    ##
    # Linux operating system detected
    ##
    echo "System $sysname (Linux) uninstalling ..."

    # Default to sysv for daemon management system
    daemon_system='sysv'
    if [ -f /sbin/init ]; then
        # Check if the file is a symbolic link
        if [ -h /sbin/init ]; then
            # Check whether linked to systemd
            file /sbin/init | grep 'systemd'
            retval=$?
            if [ $retval == "0" ]; then
                daemon_system='systemd'
            fi
        fi
    fi

    # Stop and disable the service
    if [ "$daemon_system" == "sysv" ]; then
        echo "stopping service (this might take some time)"
        service joy stop
        update-rc.d joy remove
        service joy status
    elif [ "$daemon_system" == "systemd" ]; then
        echo "stopping service (this might take some time)"
        systemctl stop joy.service
        systemctl disable joy.service
        systemctl status joy.service
    fi

    # Delete the application files
    delete_file /usr/local/bin/joy

    delete_file /usr/local/lib/libjoy.a
    delete_file /usr/local/include/joy/joy_api.h

    delete_file /usr/local/bin/sleuth

    delete_directory /usr/local/include/joy

    delete_directory /usr/local/etc/joy

    delete_directory /usr/local/share/joy
    delete_directory /usr/local/share/joy/data

    delete_file /usr/local/share/man/man1/joy.1

    # Delete daemon specific files
    if [ "$daemon_system" == "sysv" ]; then
        delete_file /etc/init.d/joy

    elif [ "$daemon_system" == "systemd" ]; then
        delete_file /etc/systemd/system/joy.service
        delete_directory /etc/systemd/system/joy.service.d
    fi

    # Uninstall the sleuth python module
    pip uninstall -y sleuth

    echo "... done"
    echo
else
    echo "error: unknown system ($sysname)"
    exit
fi
